import tkinter as tk
from tkinter import ttk
import os
import sys
from .config import Config

class WeaponConfigGUI:
    def __init__(self):
        self.root = tk.Tk()
        self.root.title("简单吃鸡")
        self.root.geometry("520x230")
        
        # 设置窗口图标
        try:
            # 获取程序运行路径
            if getattr(sys, 'frozen', False):
                # 打包后的路径
                application_path = sys._MEIPASS
            else:
                # 开发环境路径
                application_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
            
            icon_path = os.path.join(application_path, 'fb3ee-9dw9c-001.ico')
            self.root.iconbitmap(icon_path)
        except Exception as e:
            print("加载图标失败:", e)
        
        # 创建主框架
        main_frame = ttk.Frame(self.root)
        main_frame.pack(expand=True, fill="both", padx=20, pady=10)
        
        # 创建左右两个框架
        weapon1_frame = ttk.LabelFrame(main_frame, text="武器1配置")
        weapon1_frame.grid(row=0, column=0, padx=10, pady=5, sticky="nsew")
        
        weapon2_frame = ttk.LabelFrame(main_frame, text="武器2配置")
        weapon2_frame.grid(row=0, column=1, padx=10, pady=5, sticky="nsew")
        
        # 配置grid权重
        main_frame.grid_columnconfigure(0, weight=1)
        main_frame.grid_columnconfigure(1, weight=1)
        
        self._init_weapon1_frame(weapon1_frame)
        self._init_weapon2_frame(weapon2_frame)
        
        # 保存按钮
        save_button = ttk.Button(main_frame, text="保存配置", command=self._save_config)
        save_button.grid(row=1, column=0, columnspan=2, pady=10)
        
        # 使用说明
        instructions = (
            "使用说明：\n"
            "Num Lock键开启/关闭功能，数字键1，2切换武器，按住Shift键可降低灵敏度。\n"
            "PgUp/PgDn键增加/减少当前武器的微调系数。"
        )
        instruction_label = ttk.Label(
            main_frame, 
            text=instructions,
            justify=tk.LEFT,
            font=("微软雅黑", 9)
        )
        instruction_label.grid(row=2, column=0, columnspan=2, pady=10, sticky="w")
    
    def _init_weapon1_frame(self, frame):
        # 武器选择
        weapons = list(Config.guns.keys())
        ttk.Label(frame, text="武器:").grid(row=0, column=0, pady=5, padx=5, sticky="w")
        self.w1_var = tk.StringVar(value=Config.w1Name)
        w1_combo = ttk.Combobox(frame, textvariable=self.w1_var, values=weapons)
        w1_combo.grid(row=0, column=1, pady=5, padx=5, sticky="ew")
        
        # 倍镜选择
        ttk.Label(frame, text="倍镜:").grid(row=1, column=0, pady=5, padx=5, sticky="w")
        self.m1_var = tk.StringVar(value=Config.m1Name)
        self.m1_combo = ttk.Combobox(frame, textvariable=self.m1_var)
        self.m1_combo.grid(row=1, column=1, pady=5, padx=5, sticky="ew")
        
        # 绑定武器选择变化事件
        w1_combo.bind('<<ComboboxSelected>>', lambda e: self._update_mirrors(self.w1_var.get(), self.m1_combo))
        # 初始化倍镜选项
        self._update_mirrors(Config.w1Name, self.m1_combo)
    
    def _init_weapon2_frame(self, frame):
        # 武器选择
        weapons = list(Config.guns.keys())
        ttk.Label(frame, text="武器:").grid(row=0, column=0, pady=5, padx=5, sticky="w")
        self.w2_var = tk.StringVar(value=Config.w2Name)
        w2_combo = ttk.Combobox(frame, textvariable=self.w2_var, values=weapons)
        w2_combo.grid(row=0, column=1, pady=5, padx=5, sticky="ew")
        
        # 倍镜选择
        ttk.Label(frame, text="倍镜:").grid(row=1, column=0, pady=5, padx=5, sticky="w")
        self.m2_var = tk.StringVar(value=Config.m2Name)
        self.m2_combo = ttk.Combobox(frame, textvariable=self.m2_var)
        self.m2_combo.grid(row=1, column=1, pady=5, padx=5, sticky="ew")
        
        # 绑定武器选择变化事件
        w2_combo.bind('<<ComboboxSelected>>', lambda e: self._update_mirrors(self.w2_var.get(), self.m2_combo))
        # 初始化倍镜选项
        self._update_mirrors(Config.w2Name, self.m2_combo)
    
    def _update_mirrors(self, weapon_name, mirror_combo):
        """更新倍镜选项"""
        if weapon_name and weapon_name in Config.guns:
            # 直接使用武器配置中的倍镜选项，不再额外添加"none"
            mirrors = list(Config.guns[weapon_name]["mirror"].keys())
            mirror_combo['values'] = mirrors
            # 如果当前选择的倍镜不在新的选项中，重置为none
            if mirror_combo.get() not in mirrors:
                mirror_combo.set("none")

    def _save_config(self):
        # 保存武器1配置
        Config.w1Name = self.w1_var.get()
        Config.m1Name = self.m1_var.get()
        Config.k1 = 1.0  # 重置微调系数为默认值
        
        # 保存武器2配置
        Config.w2Name = self.w2_var.get()
        Config.m2Name = self.m2_var.get()
        Config.k2 = 1.0  # 重置微调系数为默认值
        
        # 更新装备
        Config.pool.submit(self._update_equipment)
        
        # 提示用户微调系数已重置
        print("配置已保存，微调系数已重置为默认值1.0，请使用PgUp/PgDn重新调整")
    
    def _update_equipment(self):
        from .equipment import Equipment
        Equipment.recognize_equipment()
    
    def run(self):
        self.root.mainloop() 